#include <QtGui>

#include "batchlist.h"

BatchList::BatchList(QWidget *parent): QListWidget(parent)
{
	setMinimumSize(640, 240);
	setAcceptDrops(true);
}

void BatchList::dragEnterEvent(QDragEnterEvent *e)
{
	e->acceptProposedAction();
}

void BatchList::dragLeaveEvent(QDragLeaveEvent *e)
{
	e->accept();
}

void BatchList::dragMoveEvent(QDragMoveEvent *e)
{
	e->acceptProposedAction();
}

void BatchList::dropEvent(QDropEvent *e)
{
	const QMimeData *mime = e->mimeData();
	
	// no url
	if (!mime->hasUrls())
		return;
	
	QList<QUrl> urls = mime->urls();
	
	// add dropped items to list
	for (int i = 0; i != urls.size(); ++i)
	{
		QString path = urls[i].toString().remove("file:///");
		// check duplication
		if (findItems(path, Qt::MatchExactly).count() == 0)
		{
			addItem(path);
		}
	}
	
	e->acceptProposedAction();
}

QStringList BatchList::pathList() const
{
	QStringList list;
	
	for (int i = 0; i != count(); ++i)
		list.append(item(i)->text());
	
	return list;
}

void BatchList::removeCurrentItem()
{
	int row = currentRow();
	
	if (row < 0)
		return;
	
	delete takeItem(row);
}

void BatchList::upItem()
{
	int row = currentRow();
	if (row < 0)
		return;
	
	if (row)
	{
		QListWidgetItem *item = takeItem(row);
		insertItem(--row, item);
		setCurrentRow(row);
	}
}

void BatchList::downItem()
{
	int row = currentRow();
	if (row < 0)
		return;
	
	if (row < count() - 1)
	{
		QListWidgetItem *item = takeItem(row);
		insertItem(++row, item);
		setCurrentRow(row);
	}
}
